<?php

class html {
	private $js = array();
	private $css = array();
	private $js_changed = false;
	private $css_changed = false;
	public $name = '';
	public $cache = true;
	private $style;
	
	public function __construct()
	{
		$this->style = new Style(true, true);
	}
	
	public function link($name, $options = array()){	
		$default = array(
			'inside' => true,
			'options' => false,
			'conditional' => false
		);		
		$default = array_merge($default, $options);	
		if(is_array($name))
		{
			foreach($name as $one){
				$options = '';
				if(is_array($default['options']) and count($default['options']))
					foreach($default['options'] as $key => $value){
						$options .= " {$key}=\"{$value}\"";
					}
				if($default['inside'])
				{
					$one = "/css/{$one}";
				}
				$this->css[] = array(
					'name' => "{$one}.css",
					'inside' => $default['inside'],
					'conditional' => $default['conditional'],
					'options' => $options
				);				
			}
		}
		else {
			$options = '';
			if(is_array($default['options']) and count($default['options']))
				foreach($default['options'] as $key => $value){
					$options .= " {$key}=\"{$value}\"";
				}
				
			if($default['inside'])
			{
				$name = "/css/{$name}";
			}
			$this->css[] = array(
				'name' => "{$name}.css",
				'inside' => $default['inside'],
				'conditional' => $default['conditional'],
				'options' =>  $options
			);
		}	
	}

	public function js($name, $options = array()){
		$default = array(
			'conditional' => false,
			'inside' => true,
			'extension' => 'js',
			'compress' => true,
			'source' => false
		);
		$default = array_merge($default, $options);		
		if(is_array($name))
		{
			foreach($name as $one){
				$name = ($default['extension'] && !$default['source']) ? "{$one}.{$default['extension']}" : $one;

				$this->js[] = array(
					'name' => ($default['inside'] && !$default['source']) ? '/js/' . $name : $name,
					'inside' => $default['inside'],
					'conditional' => $default['conditional'],
					'extension' => $default['extension'],
					'compress' =>  $default['compress'],
					'source' => $default['source']
				);				
			}
		}
		else {
			$name = ($default['extension'] && !$default['source']) ? "{$name}.{$default['extension']}" : $name;

			$this->js[] = array(
				'name' => ($default['inside'] && !$default['source']) ? '/js/' . $name : $name,
				'inside' => $default['inside'],
				'conditional' => $default['conditional'],
				'extension' => $default['extension'],
				'compress' =>  $default['compress'],
				'source' => $default['source']
			);
		}
	}	
	
	public function mootools($require, $compression = 1){
		$queryString = MOOBUILDER . '?require=';
		$queryString .= implode(',',$require);
		if($compression)
		$queryString .= "&amp;compress=1";
		$queryString .= "&.js";
		
		$this->js($queryString, array('compress' => false, 'extension' => false, 'inside' => false));
	}
	
	public function display(){
		if(!$this->name)
			return array();	
		
		if($this->cache)
		{
			$this->cssUnite();
			$this->jsUnite();
		}
		return array('scripts' => $this->js, 'links' => $this->css);
	}
	
	private function jsUnite()
	{
		if(!is_array($this->js) || !count($this->js))
			return false;
		$jss = array();
		
		foreach($this->js as $js)
		{
			if(preg_match('/^http:\/\//', $js['name']) or $js['conditional'] or !$js['compress'])
				$jss[] = $js;
			elseif(!$js['source'])
				$jss[] = $this->compressJS("." . $js['name']);
			else
				$jss[] = $this->compressJSCode($js['name']);
		}
		$this->js = $jss;
	}
	
	private function cssUnite()
	{
		if(!is_array($this->css) || !count($this->css))
			return false;
			
		$csss = array();	
		foreach($this->css as $css)
		{
			if(preg_match('/^http:\/\//', $css['name']) or $css['conditional'] or $css['options'])
				$csss[] = $css;
			else
				$csss[] = $this->style->compressCSS("." . $css['name']);
		}
		
		$this->css = $csss;
	}
	
	private function compressJS($file){
		$fileName = '';
		
		
		$fData = explode("/", $file);
		$fileName = end($fData);
	
		$cacheFile = CACHE_DIR . md5_file($file) . "-" . md5($file) . ".js";

		if(!file_exists($cacheFile))
		{
			$this->emptyCache(md5($file) . ".js");
			$script = file_get_contents($file);
		
			$script = mb_convert_encoding($script,'iso-8859-2','UTF-8');
		
			$packed = $this->compressJSCode($script);
			
			$packed =  mb_convert_encoding($packed, 'UTF-8', 'iso-8859-2');
		
			file_put_contents($cacheFile, $packed);
			return $packed;
		}
		else {
			return file_get_contents($cacheFile);
		}
	}

	private function compressJSCode($script)
	{
		$packer = new JavaScriptPacker($script, 0, true, false);
		return $packer->pack();		
	}
	
	public function clearLinks(){
		$this->css = array();
	}
	
	public function clearJs(){
		$this->js = array();
	}
	
	private function emptyCache($file)
	{
		$match = glob(CACHE_DIR . $file);
		if(is_array($match) && count($match))
		foreach($match as $m)
		{
			if(file_exists($m))
				@unlink($m);
		}
	}
	
}

?>